package com.example.bannerstuday;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

public class MainActivity2 extends AppCompatActivity {

    private EditText account;
    private EditText pws;
    private CheckBox checkBox;
    private Button button;
    private SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        account = findViewById(R.id.account);
        pws = findViewById(R.id.pws);
        checkBox = findViewById(R.id.checkBox);
        button = findViewById(R.id.button);
        //获取sharedPreference对想
        sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

        boolean isRemember = sharedPreferences.getBoolean("bol",false);
        if (isRemember){
            account.setText(sharedPreferences.getString("account",""));
            pws.setText(sharedPreferences.getString("password",""));
            checkBox.setChecked(sharedPreferences.getBoolean("bol",false));
        }

    }

    public void initView(View view){
        //写入的工具类
        SharedPreferences.Editor editor = sharedPreferences.edit();

        boolean bol = checkBox.isChecked();
        if (checkBox.isChecked()){
            String accountName = account.getText().toString();
            String password = pws.getText().toString();
            editor.putString("account",accountName);
            editor.putString("password",password);
            editor.putBoolean("bol",true);
            editor.apply();

        }else{
            editor.clear();
            editor.apply();
        }
        Intent intent = new Intent(MainActivity2.this,MainActivity.class);
        startActivity(intent);
    }
}